package cn.fyl.pet.service.impl;

import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.org.domain.Department;
import cn.fyl.pet.domain.Pet;
import cn.fyl.pet.domain.PetType;
import cn.fyl.pet.mapper.PetTypeMapper;
import cn.fyl.pet.service.IPetService;
import cn.fyl.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        // 查询所有放在map中
        List<PetType> all = petTypeMapper.loadAll();

        HashMap<Long, PetType> map = new HashMap<>();

        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }

        ArrayList<PetType> typeTree = new ArrayList<>();

        for (PetType petType : all) {
            if(petType.getPid()==null){ // 顶级部门
                typeTree.add(petType);
            }else {
                // 获取顶级部门id
                Long pid = petType.getPid();
                // 从map中获取父部门
                PetType parentPetType = map.get(pid);
                // 将自己装到  父部门 的children中
                parentPetType.getChildren().add(petType);

            }

        }

        return typeTree;
    }
}